/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * LoanCreationUI.java
 *
 * Created on 2010.11.09., 13:31:20
 */
package workshopdesign.UI;

import org.jdesktop.application.Action;
import workshopdesign.Control.DVDController;
import workshopdesign.Control.LoanController;
import workshopdesign.Model.Copy;
import workshopdesign.Model.DVD;
import workshopdesign.Model.Person;

/**
 *
 * @author Dom
 */
public class LoanCreationUI extends javax.swing.JPanel implements BaseUIController {

    private PersonListUI personListUI;
    private DVDListUI dvdListUI;
    private CopyListUI copyListUI;
    private PersonDetailUI personDetailUI;
    private DVDDetailUI dvdDetailUI;
    private Person selectedPerson = null;
    private DVD selectedDvd = null;
    private Copy selectedCopy = null;
    private LoanController loanController = LoanController.getLoanController();
    private DVDController dvdController = DVDController.getDVDControl();

    /** Creates new form LoanCreationUI */
    public LoanCreationUI() {
        initComponents();

        personListUI = new PersonListUI(this, "Select a person");
        dvdListUI = new DVDListUI(this, "Select a DVD");
        copyListUI = new CopyListUI(this, "Select a copy");
        personDetailUI = new PersonDetailUI(this, "Person details", false);
        dvdDetailUI = new DVDDetailUI(this, "DVD Details", false);

        personlist_loan_splitter.setLeftComponent(personListUI);
        dvd_copy_splitter.setTopComponent(dvdListUI);
        dvd_copy_splitter.setBottomComponent(copyListUI);
        detail_splitter.setTopComponent(personDetailUI);
        detail_splitter.setBottomComponent(dvdDetailUI);
    }

    public Copy getSelectedCopy() {
        return selectedCopy;
    }

    public void setSelectedCopy(Copy selectedCopy) {
        this.selectedCopy = selectedCopy;
    }

    public DVD getSelectedDvd() {
        return selectedDvd;
    }

    public void setSelectedDvd(DVD selectedDvd) {
        this.selectedDvd = selectedDvd;
    }

    public Person getSelectedPerson() {
        return selectedPerson;
    }

    public void setSelectedPerson(Person selectedPerson) {
        this.selectedPerson = selectedPerson;
    }

    private void check_copy_availability(Copy copy) {
        if (loanController.is_copy_available(copy)) {
            status_label.setText("COPY IS AVAILABLE FOR RENT!");
        } else {
            status_label.setText("COPY IS ALREADY RENTED OUT!");
        }
    }

    private void clear_form() {
        selectedPerson = null;
        selectedDvd = null;
        selectedCopy = null;

        personListUI.clear();
        dvdListUI.clear();
        copyListUI.clear();

        personDetailUI.setPerson(selectedPerson);
        dvdDetailUI.setDvd(selectedDvd);

        status_label.setText("");

        refresh_lists();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        personlist_loan_splitter = new javax.swing.JSplitPane();
        loan_dvd_splitter = new javax.swing.JSplitPane();
        jPanel1 = new javax.swing.JPanel();
        select_field = new javax.swing.JTextField();
        select_label = new javax.swing.JLabel();
        select_button = new javax.swing.JButton();
        status_label = new javax.swing.JLabel();
        detail_splitter = new javax.swing.JSplitPane();
        dvd_copy_splitter = new javax.swing.JSplitPane();
        jToolBar1 = new javax.swing.JToolBar();
        save_button = new javax.swing.JButton();
        cancel_button = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        refresh_button = new javax.swing.JButton();
        jSeparator2 = new javax.swing.JToolBar.Separator();
        jLabel1 = new javax.swing.JLabel();
        period_field = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();

        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        personlist_loan_splitter.setDividerLocation(300);
        personlist_loan_splitter.setName("personlist_loan_splitter"); // NOI18N

        loan_dvd_splitter.setDividerLocation(301);
        loan_dvd_splitter.setName("loan_dvd_splitter"); // NOI18N

        jPanel1.setName("jPanel1"); // NOI18N

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(workshopdesign.WorkshopDesignApp.class).getContext().getResourceMap(LoanCreationUI.class);
        select_field.setText(resourceMap.getString("select_field.text")); // NOI18N
        select_field.setName("select_field"); // NOI18N

        select_label.setText(resourceMap.getString("select_label.text")); // NOI18N
        select_label.setName("select_label"); // NOI18N

        javax.swing.ActionMap actionMap = org.jdesktop.application.Application.getInstance(workshopdesign.WorkshopDesignApp.class).getContext().getActionMap(LoanCreationUI.class, this);
        select_button.setAction(actionMap.get("select_copy_by_id")); // NOI18N
        select_button.setText(resourceMap.getString("select_button.text")); // NOI18N
        select_button.setName("select_button"); // NOI18N

        status_label.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        status_label.setText(resourceMap.getString("status_label.text")); // NOI18N
        status_label.setName("status_label"); // NOI18N

        detail_splitter.setDividerLocation(300);
        detail_splitter.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        detail_splitter.setName("detail_splitter"); // NOI18N

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(status_label, javax.swing.GroupLayout.DEFAULT_SIZE, 299, Short.MAX_VALUE)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(select_label)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(select_field, javax.swing.GroupLayout.PREFERRED_SIZE, 185, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(select_button)))
                .addContainerGap())
            .addComponent(detail_splitter, javax.swing.GroupLayout.DEFAULT_SIZE, 309, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(select_field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(select_label)
                    .addComponent(select_button))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(status_label, javax.swing.GroupLayout.PREFERRED_SIZE, 16, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(detail_splitter, javax.swing.GroupLayout.DEFAULT_SIZE, 657, Short.MAX_VALUE))
        );

        loan_dvd_splitter.setLeftComponent(jPanel1);

        dvd_copy_splitter.setDividerLocation(400);
        dvd_copy_splitter.setOrientation(javax.swing.JSplitPane.VERTICAL_SPLIT);
        dvd_copy_splitter.setName("dvd_copy_splitter"); // NOI18N
        loan_dvd_splitter.setRightComponent(dvd_copy_splitter);

        personlist_loan_splitter.setRightComponent(loan_dvd_splitter);

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jToolBar1.setName("jToolBar1"); // NOI18N

        save_button.setAction(actionMap.get("save_loan")); // NOI18N
        save_button.setText(resourceMap.getString("save_button.text")); // NOI18N
        save_button.setFocusable(false);
        save_button.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        save_button.setName("save_button"); // NOI18N
        save_button.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(save_button);

        cancel_button.setAction(actionMap.get("cancel_loan")); // NOI18N
        cancel_button.setText(resourceMap.getString("cancel_button.text")); // NOI18N
        cancel_button.setFocusable(false);
        cancel_button.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        cancel_button.setName("cancel_button"); // NOI18N
        cancel_button.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(cancel_button);

        jSeparator1.setName("jSeparator1"); // NOI18N
        jToolBar1.add(jSeparator1);

        refresh_button.setAction(actionMap.get("refresh_lists")); // NOI18N
        refresh_button.setText(resourceMap.getString("refresh_button.text")); // NOI18N
        refresh_button.setFocusable(false);
        refresh_button.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        refresh_button.setName("refresh_button"); // NOI18N
        refresh_button.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar1.add(refresh_button);

        jSeparator2.setName("jSeparator2"); // NOI18N
        jToolBar1.add(jSeparator2);

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N
        jToolBar1.add(jLabel1);

        period_field.setHorizontalAlignment(javax.swing.JTextField.TRAILING);
        period_field.setText(resourceMap.getString("period_field.text")); // NOI18N
        period_field.setName("period_field"); // NOI18N
        jToolBar1.add(period_field);

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N
        jToolBar1.add(jLabel2);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(personlist_loan_splitter, javax.swing.GroupLayout.DEFAULT_SIZE, 941, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 221, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(720, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jToolBar1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(personlist_loan_splitter, javax.swing.GroupLayout.DEFAULT_SIZE, 712, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        refresh_lists();
    }//GEN-LAST:event_formComponentShown

    @Action
    public void save_loan() {
        if ((selectedPerson == null) || (selectedCopy == null) || (period_field.getText().length() == 0)) {
            return;
        }

        if (!loanController.is_copy_available(selectedCopy)) {
            return;
        }

        try {
            int period = Integer.parseInt(period_field.getText());
            loanController.createLoan(selectedPerson, selectedCopy, period);
            clear_form();
        } catch (Exception e) {
        }
    }

    @Action
    public void cancel_loan() {
        clear_form();
    }

    @Override
    public void clear_selection() {
    }

    @Override
    public void subselection_changed() {
        Person person = personListUI.get_selected_person();
        if (person != null && person != selectedPerson) {
            selectedPerson = person;
        }

        DVD dvd = dvdListUI.get_selected_dvd();
        if ((dvd != null) && (dvd != selectedDvd)) {
            selectedDvd = dvd;
            copyListUI.set_filter_dvd(dvd);
        } else {
            dvd = selectedDvd; // To show the selected one.
        }

        Copy copy = copyListUI.get_selected_copy();
        if ((copy != null) && (copy != selectedCopy)) {
            selectedCopy = copy;
            dvd = dvdController.get_DVD_by_CopyID(copy.getId());
            check_copy_availability(copy);
        }

        if((dvd == null) && (selectedCopy != null)){
            dvd = dvdController.get_DVD_by_CopyID(selectedCopy.getId());
        }

        personDetailUI.setPerson(selectedPerson);
        dvdDetailUI.setDvd(dvd);
    }

    @Override
    public void update_data() {
        refresh_lists();
    }

    @Action
    public void refresh_lists() {
        personListUI.load_data();
        dvdListUI.load_data();
        copyListUI.load_data();
        personDetailUI.load_data();
        dvdDetailUI.load_data();
        if(selectedCopy != null){
            check_copy_availability(selectedCopy);
        }
    }

    @Action
    public void select_copy_by_id() {
        long copy_id;
        
        try {
            copy_id = Long.parseLong(select_field.getText());
        } catch (Exception e) {
            // TODO: No error message displayed.
            return;
        }

        Copy copy = dvdController.getCopy(copy_id);
        DVD dvd = dvdController.get_dvd(copy.getDVD_id());

        selectedDvd = dvd;
        dvdDetailUI.setDvd(selectedDvd);

        selectedCopy = copy;
        check_copy_availability(selectedCopy);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancel_button;
    private javax.swing.JSplitPane detail_splitter;
    private javax.swing.JSplitPane dvd_copy_splitter;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar.Separator jSeparator2;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JSplitPane loan_dvd_splitter;
    private javax.swing.JTextField period_field;
    private javax.swing.JSplitPane personlist_loan_splitter;
    private javax.swing.JButton refresh_button;
    private javax.swing.JButton save_button;
    private javax.swing.JButton select_button;
    private javax.swing.JTextField select_field;
    private javax.swing.JLabel select_label;
    private javax.swing.JLabel status_label;
    // End of variables declaration//GEN-END:variables
}
